﻿using Serilog;
using ILogger = Serilog.ILogger;

namespace WebAPI.Utils
{
    public class LoggerHelper
    {
        private static readonly ILogger Log;

        static LoggerHelper()
        {
            //
            // 初始化日志的共享实例
            Log = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .Enrich.FromLogContext()
                // 调试日志
                .WriteTo.Logger(x => x
                        .Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Debug)
                        .WriteTo.File("~/Log/debug.log", rollingInterval: RollingInterval.Day, shared: true)
                // 错误日志
                ).WriteTo.Logger(x => x
                        .Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
                        .WriteTo.File("~/Log/error.log", rollingInterval: RollingInterval.Day, shared: true)
                // 应用日志
                ).WriteTo.Logger(x => x
                    .Filter.ByIncludingOnly(e => e.Level != Serilog.Events.LogEventLevel.Debug &&
                                                 e.Level != Serilog.Events.LogEventLevel.Error)
                    .WriteTo.File("~/Log/info.log", rollingInterval: RollingInterval.Day, shared: true)
                ).CreateLogger();
        }

        /// <summary>
        /// 感兴趣的事件或与外部观察员有关的事件; 默认启用的最低日志记录级别
        /// </summary>
        /// <param name="message"></param>
        public static void Info(string message)
        {
            Log.Information(message);
        }

        /// <summary>
        /// 表示应用程序或连接系统中的故障
        /// </summary>
        /// <param name="message"></param>
        public static void Error(string message)
        {
            Log.Error(message);
        }

        /// <summary>
        /// 跟踪信息和调试细节; 通常，仅在异常情况下开启
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(string message)
        {
            Log.Debug(message);
        }

        /// <summary>
        /// 可能的问题或服务/功能退化的指标
        /// </summary>
        /// <param name="message"></param>
        public static void Warning(string message)
        {
            Log.Warning(message);
        }

        /// <summary>
        /// 导致应用程序完全失败的严重错误
        /// </summary>
        /// <param name="message"></param>
        public static void Fatal(string message)
        {
            Log.Fatal(message);
        }

        /// <summary>
        /// 跟踪信息和调试细节; 通常，仅在异常情况下开启
        /// </summary>
        /// <param name="message"></param>
        public static void Verbose(string message)
        {
            Log.Verbose(message);
        }
    }
}